import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, logout, getInfo } from '@/api/login'
const user = {
    state: {
        token: getToken(),
        user: {},
        name: '',
        roles: [],
        permissions: []
    },
    mutations: {
        SET_TOKEN(state, token) {
            state.token = token
            setToken(token)
        },
        SET_USER: (state, user) => {
            state.user = user
        },
        // SET_ROLES: (state, roles) => {
        //     state.roles = roles
        // },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_PERMISSIONS: (state, permissions) => {
            state.permissions = permissions
        },
        getUser(state, user) {
            state.user = user
            // localStorage.setItem("user", JSON.stringify(store.getters.user))
        },
        setRole(state, options) { // 切换角色，测试权限管理
            state.user = {
                role: options.role,
                permissions: options.permissions
            }
            localStorage.setItem("user", JSON.stringify(store.getters.user))
        }
    },
    actions: {
        setToken({ commit }, token) {
            return new Promise((resolve, reject) => {
                commit("SET_TOKEN", token)
                let user = {
                    username: token
                }
                commit("SET_USER", user)
                resolve()
            })
        },
        // 登录
        Login({ commit }, userInfo) {
            const username = userInfo.username.trim()
            const password = userInfo.password
            const code = userInfo.code
            const uuid = userInfo.uuid
            return new Promise((resolve, reject) => {
                login(username, password, code, uuid).then(res => {
                    commit('SET_TOKEN', res.token)
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 获取用户信息
        GetInfo({ commit, state }) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    const user = res.user
                    if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
                        commit('SET_ROLES', res.roles)
                        commit('SET_PERMISSIONS', res.permissions)
                    } else {
                        commit('SET_ROLES', ['ROLE_DEFAULT'])
                    }
                    commit('SET_NAME', user.userName)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 退出系统
        LogOut({ commit, state }) {
            return new Promise((resolve, reject) => {
                logout(state.token).then(() => {
                    commit('SET_TOKEN', '')
                    commit('SET_ROLES', [])
                    commit('SET_PERMISSIONS', [])
                    removeToken()
                    resolve()
                }).catch(error => {
                    reject(error)
                })
            })
        },
        // 获取用户信息
        getUser({ commit }, token) {
            commit("getUser", token)
        },
        setRole({ commit }, options) {
            commit("setRole", options)
        },
        // 登出
        logOut({ commit }) {
            return new Promise((resolve, reject) => {
                commit('SET_TOKEN', '')
                // commit('SET_ROLES', [])
                removeToken()
                resolve()
            })
        },
    },
}
export default user